//
//  FQDTabBarCell.m
//  FakeQDaily
//
//  Created by mewxiv on 9/25/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDTabBarCell.h"

@interface FQDTabBarCell ()

@property (nonatomic, strong) NSMutableArray *tabButtons;
@property (nonatomic, strong) UIView *indicatorLine;

@end

@implementation FQDTabBarCell
@dynamic delegate;

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _selectedIndex = 0;
        _tabButtons = [NSMutableArray array];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self updateCell];
    [self updateIndicatorLine];
}

- (void)updateCell
{
    CGFloat viewWidth = self.bounds.size.width;
    NSInteger buttonsCount = [self.tabButtons count];
    CGFloat buttonWidth = viewWidth / buttonsCount;
    CGFloat buttonHeight = self.bounds.size.height;
    CGFloat x = 0;
    CGFloat y = 0;
    for (NSInteger i = 0; i < buttonsCount; i++) {
        CGRect buttonFrame = CGRectMake(x, y, buttonWidth, buttonHeight);
        UIButton *button = self.tabButtons[i];
        button.frame = buttonFrame;
        x += buttonWidth;
    }
}

- (void)tabButtonTapped:(UIButton *)button
{
    NSInteger tabIndex = [self.tabButtons indexOfObject:button];
    NSString *tabTitle = [self.tabTitles objectAtIndex:tabIndex];
    if ([self.delegate respondsToSelector:@selector(didSelectTabButton:atTabIndex:preTabIndex:title:)]) {
        [self.delegate didSelectTabButton:button atTabIndex:tabIndex preTabIndex:self.selectedIndex title:tabTitle];
    }
    self.selectedIndex = tabIndex;
}

- (void)setTabTitles:(NSArray *)tabTitles
{
    _tabTitles = tabTitles;
    
    NSInteger titlesCount = [tabTitles count];
    NSInteger buttonsCount = [self.tabButtons count];
    for (NSInteger i = titlesCount; i < buttonsCount; i++) {
        UIButton *button = self.tabButtons[i];
        [button removeFromSuperview];
    }
    if (buttonsCount > titlesCount) {
        [self.tabButtons removeObjectsInRange:NSMakeRange(titlesCount, buttonsCount-titlesCount)];
    }
    
    for (NSInteger i = 0; i < titlesCount; i++) {
        if (i >= buttonsCount) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.titleLabel.font = FQDRegularFontWithSize(14.0f);
            [button addTarget:self action:@selector(tabButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:button];
            [self.tabButtons addObject:button];
        }
        NSString *title = tabTitles[i];
        UIButton *button = self.tabButtons[i];
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    [self setNeedsLayout];
}

- (void)updateIndicatorLine
{
    UIButton *selectTabButton = self.tabButtons[self.selectedIndex];
    if (selectTabButton) {
        CGPoint center = CGPointMake(selectTabButton.center.x, selectTabButton.frame.size.height - 2);
        self.indicatorLine.center = center;
        self.indicatorLine.hidden = NO;
    } else {
        self.indicatorLine.hidden = YES;
    }
}

- (void)setSelectedIndex:(NSInteger)selectedIndex
{
    UIButton *preSelectTabButton = self.tabButtons[_selectedIndex];
    UIButton *selectTabButton = self.tabButtons[selectedIndex];
    [preSelectTabButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [selectTabButton setTitleColor:RGB(136, 136, 136) forState:UIControlStateNormal];
    
    _selectedIndex = selectedIndex;
    
}

- (UIView *)indicatorLine
{
    if (!_indicatorLine) {
        CGRect lineFrame = CGRectMake(0, 0, 100, 2);
        _indicatorLine = [[UIView alloc] initWithFrame:lineFrame];
        _indicatorLine.backgroundColor = FQDHighlightColor;
        _indicatorLine.hidden = YES;
        [self addSubview:_indicatorLine];
    }
    return _indicatorLine;
}

+ (NSString *)reuseId
{
    return @"FQDTabBarCell";
}

@end
